package cc.eddic.pratice.programmingparadigms.emp01array.impl;

import cc.eddic.pratice.programmingparadigms.Employee;
import cc.eddic.pratice.programmingparadigms.Result;
import cc.eddic.pratice.programmingparadigms.emp01array.EmployeeStatisticsService;

public class StatisticsTyqImpl extends ReaderGcsImpl implements EmployeeStatisticsService {
    /**
     * @return 从Excel读取数据
     */
    public Employee[] readFromExcel() {
        return new Employee[]{
                new Employee("曾奕哲", "男", "SIE", "General Staff"),
                new Employee("李馨", "女", "JP APP2 GP1", "软件工程师"),
                new Employee("谭雨晴", "女", "PLM Service Dept", "软件工程师"),
                new Employee("韩睿洁", "女", "SSS", "Salesforce解决方案工程师"),
                new Employee("李惠", "女", "ERP", "SAP运维开发工程师"),
                new Employee("王巧家", "男", "ERP", "SAP运维开发工程师"),
                new Employee("孙晨玮", "男", "Enterprise Common Service Devision", "SAP运维开发工程师"),
                new Employee("崔相元", "男", "Procurement Solution", "云计算开发工程师")
        };
    }

    /**
     * @return 按照性别统计
     */
    @Override
    public Result[] pivotByGender(Employee[] employees) {
//        throw new UnsupportedOperationException();
        Result resultWoman = new Result("女", 0);
        Result resultMan = new Result("男", 0);
        for (Employee employee : employees) {
            if (employee.getGender().equals("女")) {
                resultWoman.setCount(resultWoman.getCount() + 1);
            } else {
                resultMan.setCount(resultMan.getCount() + 1);
            }
        }
        return new Result[]{resultWoman, resultMan};

    }

    /**
     * @return 按照部门统计
     */
    public Result[] pivotByDepartment(Employee[] employees) {
        Result[] results = new Result[employees.length];
        int index = 0;
        for (Employee employee : employees) {
            boolean isFound = false;
            for (Result result : results) {
                if (null == result) {
                    continue;
                }
                if (employee.getDepartment().equals(result.getKey())) {
                    result.setCount(result.getCount() + 1);
                    isFound = true;
                    break;
                }
            }
            if (!isFound) {
                results[index] = new Result(employee.getDepartment(), 1);
                index++;
            }
        }
        Result[] FinalResult = new Result[index];
        for (int i = 0; i < index; i++) {
            FinalResult[i] = new Result(results[i].getKey(), results[i].getCount());
        }
        Reverse(FinalResult);
        return FinalResult;
    }

    /**
     * @return 按照部门统计
     */
    public Result[] pivotByJobTitle(Employee[] employees) {
        Result[] results = new Result[employees.length];
        int index = 0;
        for (Employee employee : employees) {
            boolean isFound = false;
            for (Result result : results) {
                if (null == result) {
                    continue;
                }
                if (employee.getJobTitle().equals(result.getKey())) {
                    result.setCount(result.getCount() + 1);
                    isFound = true;
                    break;
                }
            }
            if (!isFound) {
                results[index] = new Result(employee.getJobTitle(), 1);
                index++;
            }
        }
        Result[] FinalResult = new Result[index];
        for (int i = 0; i < index; i++) {
            FinalResult[i] = new Result(results[i].getKey(), results[i].getCount());
        }
        Reverse(FinalResult);
        return FinalResult;
    }

    //倒序排序
    private static void Reverse(Result[] results) {
        for (int i = results.length - 1; i > 0; i--) {
            for (int j = results.length - 1; j > results.length - 1 - i; j--) {
                if (results[j].getCount() > results[j - 1].getCount()) {
                    Result result = results[j];
                    results[j] = results[j - 1];
                    results[j - 1] = result;
                }
            }
        }
    }
}
